import { showNotify, showConfirmDialog, showFailToast,showSuccessToast,showImagePreview } from 'vant';

const warning = (options = {}) =>
{
    let message = typeof options === 'string' ? options : '未知信息';
    let duration = 1000;
    let onClose = () => {}

    if(typeof options === 'object')
    {
        message = options.message ?? message;
        duration = options.duration ?? duration;
        onClose = typeof options.onClose === 'function' ? options.onClose : onClose;
    }

    showNotify({
        type:'warning',
        message:message,
        onClose:onClose
    })
}

const success = (options = {}) =>
{
    let message = typeof options === 'string' ? options : '未知信息';
    let duration = 1000;
    let onClose = () => {}

    if(typeof options === 'object')
    {
        message = options.message ?? message;
        duration = options.duration ?? duration;
        onClose = typeof options.onClose === 'function' ? options.onClose : onClose;
    }

    showNotify({
        type:'success',
        message:message,
        duration:duration,
        onClose:onClose
    })
    
}
const danger = (options = {}) => {
    let message = typeof options === 'string' ? options : '请求错误';
    let duration = 2000;
    let onClose = () => { }

    if (typeof options === 'object') {
        message = options.message ?? message;
        duration = options.duration ?? duration;
        onClose = typeof options.onClose === 'function' ? options.onClose : onClose;
    }

    showNotify({
        type: 'danger',
        message: message,
        duration: duration,
        onClose: onClose
    })
}

export default {
    install(app)
    {
        app.config.globalProperties.$warning = warning;
        app.config.globalProperties.$success = success;
        app.config.globalProperties.$error = danger;
        app.config.globalProperties.$confirm = showConfirmDialog;
        app.config.globalProperties.$fail = showFailToast;
        app.config.globalProperties.$showSuccessToast = showSuccessToast;
        app.config.globalProperties.$showImage = showImagePreview;
    }
}